package com.bym.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bym.mall.consts.MallConst;
import com.bym.mall.entity.Category;
import com.bym.mall.mapper.CategoryMapper;
import com.bym.mall.service.ICategoryService;
import com.bym.mall.vo.CategoryVo;
import com.bym.mall.vo.ResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author bym
 * @since 2022-03-14
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public ResponseVo<List<CategoryVo>> categories() {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<Category> categories = categoryMapper.selectList(queryWrapper);
        List<CategoryVo> lists = getSubCategories(categories, MallConst.ROOT_PARENT_ID);
        return ResponseVo.success(lists);
    }

    /**
     * 获取根目录的子目录
     *
     * @param categories 类目录集合
     * @param parentId   根id
     * @return {@link List}<{@link CategoryVo}>
     */
    private List<CategoryVo> getSubCategories(List<Category> categories, Integer parentId) {
        List<CategoryVo> subCategories = new ArrayList<>();
        for (Category category : categories) {
            if (category.getParentId().equals(parentId)) {
                CategoryVo categoryVo = new CategoryVo();
                BeanUtil.copyProperties(category, categoryVo);
                subCategories.add(categoryVo);
                categoryVo.setSubCategories(getSubCategories(categories, categoryVo.getId()));
            }
        }
        categoriesSort2(subCategories);
        return subCategories;
    }

    /**
     * 根据sort_order对类目录集合排序（大->小）
     *
     * @param categories 类目录集合
     */
    private void categoriesSort(List<CategoryVo> categories) {
        categories.sort((c1, c2) -> Integer.compare(c2.getSortOrder(), c1.getSortOrder()));
    }

    /**
     * 根据主键id对类目录集合排序（小->大）
     *
     * @param categories 类别
     */
    private void categoriesSort2(List<CategoryVo> categories) {
        categories.sort((c1, c2) -> Integer.compare(c1.getId(), c2.getId()));
    }

    @Override
    public Set<Integer> findSubCategoryIds(Integer parentId) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<Category> categories = categoryMapper.selectList(queryWrapper);
        Set<Integer> resultSet = new LinkedHashSet<>();
        getSubSubCategoryIds(categories, parentId, resultSet);
        return resultSet;
    }

    /**
     * 获取根目录的子目录id
     *
     * @param categories 类目录集合
     * @param parentId   根id
     * @return {@link List}<{@link CategoryVo}>
     */
    private void getSubSubCategoryIds(List<Category> categories, Integer parentId, Set<Integer> resultSet) {
        for (Category category : categories) {
            if (category.getParentId().equals(parentId)) {
                resultSet.add(category.getId());
                getSubSubCategoryIds(categories, category.getId(), resultSet);
            }
        }
    }
}
